package com.ttn.search.bus;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;

import com.ttn.entities.Connection;

/**
 * This service is designed to deliver functios used to search connections
 * 
 * @author robert eberhardt
 *
 */
@Service
public class SearchService  extends HibernateDaoSupport{
	
	
	public Connection getConnection(int connectionId){
		return (Connection) this.getSession().load(Connection.class, Integer.valueOf(connectionId));
	}
	
	public List<Connection> search(SearchBean command){
		Criteria criteria = this.getSession().createCriteria(Connection.class);
		criteria.add(Restrictions.eq("from", command.getFrom()));
		criteria.add(Restrictions.eq("to", command.getTo()));		
		if (command.getTripDate() != null){
			criteria.add(Restrictions.eq("tripDate", command.getTripDate()));
		}else{
			criteria.add(Restrictions.gt("tripDate", new Date()));	
		}
		return criteria.list();
	}
	
	
	public SearchBean createEmptySearchBean(){
		return new SearchBean();
	}
	

	@Autowired
	public void init(SessionFactory factory) {
	    setSessionFactory(factory);
	}
	

}
